package com.quanta.anag.candidati.dynamicjsf;

import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.UIColumn;
import java.util.ArrayList;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;

/**
 *
 * @author roberto
 */
public class JsfDataTable {

    private String styleClass;
    private String header;
    private String value;
    private Class<?> valueClass;
    private String headerClass;
    private String rowClasses;
    private String columnClasses;
    private List<JsfDataTableColumn> colonne = new ArrayList<JsfDataTableColumn>();
    private String varName;

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setValue(String value, Class<?> valueClass) {
        this.value = value;
        this.valueClass = valueClass;
    }

    public void setHeaderClass(String headerClass) {
        this.headerClass = headerClass;
    }

    public void setRowClasses(String rowClasses) {
        this.rowClasses = rowClasses;
    }

    public void setColumnClasses(String columnClasses) {
        this.columnClasses = columnClasses;
    }

    public void setVarName(String varName) {
        this.varName = varName;
    }

    public void addColumn(JsfDataTableColumn jsfDataTableColumn) {
        colonne.add(jsfDataTableColumn);
    }

    public HtmlDataTable createHtmlDataTable() {
        HtmlDataTable dataTable = new HtmlDataTable();
        dataTable.setValueExpression("value", createValueExpression(value, valueClass));

        dataTable.setVar(varName);

        if (styleClass != null && styleClass.length() > 0) {
            if (styleClass.contains("#{")) {
                dataTable.setValueExpression("styleClass", createValueExpression(styleClass, String.class));
            } else {
                dataTable.setStyleClass(styleClass);
            }
        }

        if (headerClass != null && headerClass.length() > 0) {
            if (headerClass.contains("#{")) {
                dataTable.setValueExpression("headerClass", createValueExpression(headerClass, String.class));
            } else {
                dataTable.setHeaderClass(headerClass);
            }
        }
        if (rowClasses != null && rowClasses.length() > 0) {
            if (rowClasses.contains("#{")) {
                dataTable.setValueExpression("rowClasses", createValueExpression(rowClasses, String.class));
            } else {
                dataTable.setRowClasses(rowClasses);
            }
        }
        if (columnClasses != null && columnClasses.length() > 0) {
            if (columnClasses.contains("#{")) {
                dataTable.setValueExpression("columnClasses", createValueExpression(columnClasses, String.class));
            } else {
                dataTable.setColumnClasses(columnClasses);
            }
        }


        for (JsfDataTableColumn jsfDataTableColumn : colonne) {
            UIColumn uic = jsfDataTableColumn.createUIColumn();
            dataTable.getChildren().add(uic);
        }

        return dataTable;
    }

    private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createValueExpression(
                facesContext.getELContext(), valueExpression, valueType);
    }
}
